import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login';
import Main from '@/views/Main';
import {useStore} from "vuex";
import {ElMessage} from "element-plus";

const routes = [
  {path: '/', name: 'Login', component: Login},
  {
    path: '/Main', name: 'Main', component: Main,
    redirect: '/DashBoard',
    children: [
      {path: '/DashBoard', name: 'DashBoard', component:()=>import('@/views/DashBoard')},
      {path: '/rms/Room', name: 'Room', component: () => import('@/views/rms/Room')},
      {path: '/rms/RoomInsert', name: 'RoomInsert', component: () => import('@/views/rms/RoomInsert')},
      {path: '/rms/RoomUpdate', name: 'RoomUpdate', component: () => import('@/views/rms/RoomUpdate')},
      {path: '/rms/School', name: 'School', component: () => import('@/views/rms/School')},
      {path: '/rms/SchoolInsert', name: 'SchoolInsert', component: () => import('@/views/rms/SchoolInsert')},
      {path: '/rms/SchoolUpdate', name: 'SchoolUpdate', component: () => import('@/views/rms/SchoolUpdate')},
      {path: '/rms/Assets', name: 'Assets', component: () => import('@/views/rms/Assets')},
      {path: '/rms/AssetsInsert', name: 'AssetsInsert', component: () => import('@/views/rms/AssetsInsert')},
      {path: '/rms/AssetsUpdate', name: 'AssetsUpdate', component: () => import('@/views/rms/AssetsUpdate')},
      {path: '/ums/Dept', name: 'Dept', component: () => import('@/views/ums/Dept')},
      {path: '/ums/DeptInsert', name: 'DeptInsert', component: () => import('@/views/ums/DeptInsert')},
      {path: '/ums/DeptUpdate', name: 'DeptUpdate', component: () => import('@/views/ums/DeptUpdate')},
      {path: '/ums/Emp', name: 'Emp', component: () => import('@/views/ums/Emp')},
      {path: '/ums/EmpInsert', name: 'EmpInsert', component: () => import('@/views/ums/EmpInsert')},
      {path: '/ums/EmpUpdate', name: 'EmpUpdate', component: () => import('@/views/ums/EmpUpdate')},
      {path: '/ums/Role', name: 'Role', component: () => import('@/views/ums/Role')},
      {path: '/ums/RoleInsert', name: 'RoleInsert', component: () => import('@/views/ums/RoleInsert')},
      {path: '/ums/RoleUpdate', name: 'RoleUpdate', component: () => import('@/views/ums/RoleUpdate')},
      {path: '/ums/EmpRoles', name: 'EmpRoles', component: () => import('@/views/ums/EmpRoles')},
      {path: '/ums/RolePermissions', name: 'RolePermissions', component: () => import('@/views/ums/RolePermissions')},
      {path: '/Personal', name: 'Personal', component: () => import('@/views/ums/personal/Personal.vue')},
      {path: '/PersonalUpdate', name: 'PersonalUpdate', component: () => import('@/views/ums/personal/PersonalUpdate')},
      {path: '/UpdatePassword', name: 'UpdatePassword', component: () => import('@/views/ums/personal/UpdatePassword')},
      {path: '/ums/Permission', name: 'Permission', component: () => import('@/views/ums/Permission')},
      {path: '/ums/PermissionInsert', name: 'PermissionInsert', component: () => import('@/views/ums/PermissionInsert')},
      {path: '/ums/PermissionUpdate', name: 'PermissionUpdate', component: () => import('@/views/ums/PermissionUpdate')},

    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

/*
 * 路由前置守卫：每次转发路由前执行的函数
 * param to: 来源地址
 * param from: 目标地址
 * next: 放行函数
 */
router.beforeEach((to, from, next) => {
  const vuex = useStore();
  // 登录页面或已登录状态，均直接放行
  if (to.path === '/' || vuex.state['loginFlag']) {
    next();
  }
  // 其余情况直接跳转回登录页面
  else {
    ElMessage.warning('请先登录！');
    setTimeout(() => next('/'), 1000);
  }
});

export default router



